package com.stable.web.controller;

import com.stable.model.*;
import com.stable.service.EastHsgtcgService;
import com.stable.utils.DateUtil;
import com.stable.utils.FileUtil;
import com.stable.utils.TradeDateUtil;
import com.stable.vo.EastHsgtcg;
import com.stable.vo.EastHsgtcgDetail;
import com.stable.vo.bus.HorizontalInfo;
import com.stable.vo.http.JsonResult;
import com.stable.vo.spi.req.EsQueryPageReq;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 沪深港通持股
 */
@RequestMapping("/eastHsgtcg")
@RestController
@Log4j2
public class EastHsgtcgController {

    @Autowired
    private EastHsgtcgService eastHsgtcgService;


    /**
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> stocklist(String queryDate,
                                                String code,
                                                String hyname,
                                                EsQueryPageReq queryPage) {
        log.info("code:{},hyname:{}", code, hyname);
        queryPage.setPageSize(200);
        if (StringUtils.isEmpty(queryPage.getSortName())) {
            queryPage.setSortName("LTZB");
        }
        if (StringUtils.isEmpty(queryDate)) {
            queryDate = TradeDateUtil.getNearTradeDate(0);
        }
        JsonResult r = new JsonResult();
        try {
            r.setResult(eastHsgtcgService.queryList(queryDate, code, hyname, queryPage));
            r.setStatus(JsonResult.OK);
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(JsonResult.ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }


    /**
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(EastHsgtcg eastHsgtcg) {
        eastHsgtcgService.editMark(eastHsgtcg.getCode(), eastHsgtcg);
        Map<String, String> map = new HashMap<>();
        map.put("result", "提交成功");
        return map;
    }


    /**
     * 下载估值文件
     *
     * @return
     */
    @RequestMapping({"/downloadGuZhi"})
    public void downloadGuZhi(String code, String name) {
        HSSFWorkbook workbook = WorkBookCommon.workBook(code);

        FileUtil.downloadWorkBook(workbook, code + "_" + name + ".xls");
        log.info("download xls for:code:{},name:{}", code, name);
    }


    /**
     * @return
     */
    @RequestMapping(value = "/analyseHsgtcg", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> analyseHsgtcg(String queryDate,
                                                    String code,
                                                    String name,
                                                    String size,
                                                    EsQueryPageReq queryPage) {
        log.info("code:{},name:{},size:{}", code, name, size);
        queryPage.setPageSize(200);
        if (StringUtils.isEmpty(queryDate)) {
            queryDate = TradeDateUtil.getNearTradeDate(0);
        }
        JsonResult r = new JsonResult();
        try {
            int parseSize = Integer.parseInt(size);
            r.setResult(eastHsgtcgService.analyseHsgtcg(queryDate, code, name, parseSize, queryPage));
            r.setStatus(JsonResult.OK);
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(JsonResult.ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }


    /**
     * 下载文件
     *
     * @return
     */
    @RequestMapping({"/downloadList"})
    public void downloadList(String queryDate,
                         String code,
                         String name,
                         String size,
                         EsQueryPageReq queryPage) {
        queryPage.setPageSize(200);
        if (StringUtils.isEmpty(queryDate)) {
            queryDate = TradeDateUtil.getNearTradeDate(0);
        }

        int parseSize = Integer.parseInt(size);
        List<EastHsgtcgDetail> eastHsgtcgDetails = eastHsgtcgService.analyseHsgtcg(queryDate, code, name, parseSize, queryPage);

        String filename = queryDate+"_沪深港通列表.txt";
        FileUtil.downloadFile(eastHsgtcgDetails.stream().map(EastHsgtcgDetail::getCode).collect(Collectors.toList()), filename);
    }
}
